<?php 
class Lib_Application_Resource_Acl extends Zend_Application_Resource_ResourceAbstract
{
    const ACL_ALLOW = 'allow';
    const ACL_DENY  = 'deny';

    const SRC_MVC   = 'mvc';
    const SRC_RES   = 'res';
    const WILDCARD  = '*';

    public static $PATTERN_DELIM = ':';
    public static $SUBPATTERN_DELIM = ',';

    /**
     * @var Zend_Acl
     */
    private $_acl;

	public function init(){
        $aclManager = new \Managers\Acl();
        print '<pre>';
        print_r($aclManager->getRolesList());
        print '</pre>';
        die;
        return $this->_acl;
	}

    /**
     * Roles Initialisation
     *
     * @return void
     */
    private function _initRoles(){
        // Reading default roles
        $rolesConfig = new Zend_Config_Ini(APPLICATION_PATH . '/configs/acl.ini', APPLICATION_ENV);
        
        $roles = array();
        if($rolesConfig->acl->roles){
            $roles = $rolesConfig->acl->roles->toArray();
        }

        // Adding roles
        foreach($roles as $roleName=>$role){
            $parents = null;
            // Set role extenders
            if(isset($role['extends']) && is_array($role['extends']) && reset($role['extends'])){
                $parents = $role['extends'];
            }
            $this->_acl->addRole($roleName, $parents);
        }
    }

    /**
     * Available resource initialisation
     * @return void
     */
    private function _initResources(){
        $options = $this->getOptions();
        // Adding resources to ACL
        foreach($options as $option){
            // Parsing ACL options
            $option = explode(self::$PATTERN_DELIM, $option);
            $access     = $option[0];
            $role       = (isset($option[1]) && $option[1])?(($option[1] != self::WILDCARD)? $option[1] : null):null;
            $resource   = (isset($option[2]) && $option[2])?(($option[2] != self::WILDCARD)? $option[2] : null):null;
            $privilege  = (isset($option[3]) && $option[3])?(($option[3] != self::WILDCARD)? explode(self::$SUBPATTERN_DELIM,$option[3]): null):null;

            // Adding resources if the weren't created
            if(null !== $resource && !$this->_acl->has($resource)){
                $this->_acl->addResource($resource);
            }

            // Adding rules
            switch($access){
                case self::ACL_ALLOW :
                    $this->_acl->allow($role, $resource, $privilege);
                break;

                case self::ACL_DENY :
                    $this->_acl->deny($role, $resource, $privilege);
                break;

                default:
                break;
            }
        }
    }

    /**
     * @param \Zend_Acl $acl
     * @return void
     */
    public function setAcl($acl)
    {
        $this->_acl = $acl;
    }

    /**
     * @return \Zend_Acl
     */
    public function getAcl()
    {
        return $this->_acl;
    }
}